package com.flying.pas.security;

import java.util.List;

import com.flying.framework.application.Application;
import com.flying.framework.security.Principal;
import com.flying.pas.security.model.Perm;
import com.flying.pas.security.model.User;
import com.flying.pas.security.service.PermService;

public class AuthUser implements Principal{
	private static final long serialVersionUID = -4739579737873262575L;
	
	private final User user;
	private List<Perm> perms;
	
	public AuthUser(User data) {
		this.user = data;
	}
	
	@Override
	public String getId() {
		return String.valueOf(user.getUserId());
	}

	@Override
	public String getName() {
		return user.getUsername();
	}

	@Override
	public boolean isRole(String[] roles) {
		return true;
	}

	@Override
	public boolean hasPermission(String[] permissions) {
		return true;
	}

	public List<Perm> getPerms() throws Exception {
		if(this.perms == null) {
			PermService ps = Application.getInstance().getModules().getLocalModule("pas").getService(PermService.class);
			this.perms = ps.findByUserId(user.getUserId()).getRows();
		}
		return this.perms;
	}

	public User getUser() {
		return user;
	}

	@Override
	public String getOrg() {
		return user.getOrgId()+"";
	}
}
